// pages/finance/basicList/basicList/basicList.js
import {
  basicTable,
  basicDownload,
  sendBasicTableCalculate
} from '../../../../network/finance'
import {
  toast
} from '../../../../utils/Tools'
import {
  formatTime
} from '../../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '选择时间',
    // 设置标题
    basicTitle: [],
    // 列表内容
    baseTableValue: [],
    // 下载链接
    excelDownload: "",
    // 下拉选择开关
    open: false,
    index: -1,
    chooseLists: [],
  
    colorIndex: 0,
    //标题数组键值
    chooseBase: [],
    // 标题数组键名
    customerList: [],
    indexList: [],
    allList: {}

  },
  // 点击栏目项触发的函数
  chooseList(e) {
    let chooseLists = this.data.chooseLists;//标题名数组
    let value = e.currentTarget.dataset.value;//获取当前点击的标题中文名
    let colorIndex = this.data.colorIndex;
    let index = e.currentTarget.dataset.index;//获取点击项的索引
    // 查找数组中的元素
    if (chooseLists.indexOf(value) == -1) {//判断标题名数组中是否存在点击的标题
      chooseLists[index] = value//不存在就添加进去
    } else {
      delete chooseLists[index];//已存在就代表取消选择,所以删除
    }
    console.log("chooseLists",chooseLists[index]);
    console.log("index",index);
    this.setData({
      // 更新数据
      chooseLists,
      colorIndex,
    })
  },
  // 栏目提交按钮函数
  sumbitBtn() {
    let customerList = this.data.customerList;//全部栏目英文名称
    let indexList = this.data.indexList;//选中标题的索引
    let allList = this.data.allList;//英文名称:1
    let chooseLists = this.data.chooseLists;//选中标题的索引:中文名
    // 将用户选择的标题索引push进变量indexList中
    for (var key in chooseLists) {
      indexList.push(+key)
    }
    if (indexList.length == 0) {//选择索引为0时代表一个都没选择,提示用户选择
      wx.showToast({
        title: '请选择内容',
        icon: 'none',
        duration: 1500
      })
    } else {
      for (var key in indexList) {
        // 传入1则显示这个列
        allList[customerList[key]] = 1
      }
      this.setData({
        allList,
      })
      // 重新调用网络请求
      this._basicTable()
    }

  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let date = formatTime(new Date())
    this.setData({
      date
    })
    // 获取列表数据
    this._basicTable(),
      this._basicDownload()
  },
  // 展示和隐藏下拉选择
  selectSwitch() {
    this.setData({
      open: !this.data.open
    })
  },
  // 基础表提交计算
  // basic_table_calculate
  async _sendBasicTableCalculate() {
    let data = {
      start_date: this.data.date
    }
    const res = await sendBasicTableCalculate(data)
    if (res.code === 200) {
      toast("计算成功", 'success')
    } else {
      toast(res.message)
    }
  },
  // 下载按钮
  load: function () {
    wx.downloadFile({
      url: `http://master.gddzxcx.com/${this.data.excelDownload}`,
      success: function (res) {
        var filePath = res.tempFilePath;
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            toast("下载文档成功", 'success')
          }
        })
      }
    })
  },


  // 基础表下载
  async _basicDownload() {
    const res = await basicDownload()
    if (res.code === 200) {
      this.setData({
        excelDownload: res.data
      })
    }
  },
  //网络请求//
  async _basicTable() {
    let allList = this.data.allList;
    let data = {
      start_date: this.data.date
    }
    data = Object.assign(data, allList)
    const res = await basicTable(data)
    console.log("res",res);
    if (res.code === 200) {
      let chooseBase = this.data.chooseBase;
      let customerList = this.data.customerList;
      customerList = Object.keys(res.data.basic_field.map)//获取标题数组的键名(英文)
      chooseBase = Object.values(res.data.basic_field.map);//获取标题数组的键值(中文)
     
      this.setData({
        // 内容{agreement_code: "DZ-20210205150",arriver_date: "2021-02-05"}
        baseTableValue: res.data.base_table_value,
        // 标题{agreement_code: "唯一码",arriver_date: "到货确认日期"}
        basicTitle: res.data.basic_field.map,
        chooseBase,   //"唯一码", "下单日期", "到货确认日期", 
        customerList, //"agreement_code", "make_date", "arriver_date",
        open: true,
      })
    }
    console.log("customerList",this.data.customerList);
  }
})